package com.lijin.batchsys.framework.dao.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.lijin.batchsys.framework.common.AssertUtil;
import com.lijin.batchsys.framework.common.BatchSysConstants;

public class JobInfo {

	private int id;
	private int batchNo;
	private String batchDesc;
	private int jobNo;
	private String jobDesc;
	private String jobCtrlFlgs; // 任务控制位 BitMap
	private String chJobCtrlFlgs; //汉字表达
	private char[] ctrlFlgsArr; // 任务控制位数组
	private String recSt;
	
	private List<StepInfo> stepList = new ArrayList<StepInfo>();
	
	/**
	 * 为任务添加一个步骤
	 * @param step
	 */
	public void addStep(StepInfo step) {
		stepList.add(step);
	}
	
	public List<StepInfo> getStepList() {
		return stepList;
	}
	
	public String getSummary() {
		StringBuilder summary = new StringBuilder();
		summary.append("[batch:" + this.batchNo + "-" + this.batchDesc + ",");
		summary.append("job:" + this.jobNo + "-" + this.jobDesc + "]");
		return summary.toString();
	}
	
	/**
	 * 获取任务下的步骤总数
	 * @return
	 */
	public int getStepNum() {
		return stepList == null ? 0 : stepList.size();
	}
	
	public int getJobNo() {
		return jobNo;
	}

	public void setJobNo(int jobNo) {
		this.jobNo = jobNo;
	}

	public int getBatchNo() {
		return batchNo;
	}
	
	public void setBatchNo(int batchNo) {
		this.batchNo = batchNo;
	}
	
	public String getChJobCtrlFlgs() {
		return chJobCtrlFlgs;
	}
	
	public void setChJobCtrlFlgs(String chJobCtrlFlgs) {
		this.chJobCtrlFlgs = chJobCtrlFlgs;
	}
	
	public void setJobCtrlFlgs(String jobCtrlFlgs) {
		// 补全32位，不足32位则右补0
		this.jobCtrlFlgs = StringUtils.rightPad(jobCtrlFlgs.trim(), 32, '0');
		this.ctrlFlgsArr = this.jobCtrlFlgs.toCharArray();
	}
	
	public boolean isEnabled() {
		return BatchSysConstants.REC_VALID.equals(recSt);
	}
	
	/**
	 * 根据控制位第一位来判断Job下的Step是并行执行还是串行执行
	 * 0 - 串行
	 * 1 - 并行
	 * @return
	 */
	public boolean isConcurrent() {
		AssertUtil.objIsNull(ctrlFlgsArr, "控制位数组未初始化");
		return ctrlFlgsArr[0] == '1';
	}

	/**
	 * 根据控制位第二位来判断Job是否可跳过
	 * 1 - 可跳过
	 * 0 - 整个批次停止
	 * @return
	 */
	public boolean canSkip() {
		AssertUtil.objIsNull(ctrlFlgsArr, "控制位数组未初始化");
		return ctrlFlgsArr[1] == '1';
	}
	
	public String getJobCtrlFlgs() {
		return jobCtrlFlgs;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	public String getJobDesc() {
		return jobDesc;
	}
	public void setJobDesc(String jobDesc) {
		this.jobDesc = jobDesc;
	}

	public String getRecSt() {
		return recSt;
	}

	public void setRecSt(String recSt) {
		this.recSt = recSt;
	}

	public String getBatchDesc() {
		return batchDesc;
	}

	public void setBatchDesc(String batchDesc) {
		this.batchDesc = batchDesc;
	}
}
